package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.globle.GlideEngine;
import com.zlzxm.shoesmarket.util.AndroidVersionUtil;
import com.zlzxm.shoesmarket.util.ZFileUtil;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMReleaseRecruit;

import java.util.List;


/**
 * Created by zlz
 * on  4/24/21
 */
public class ACReleaseRecruit extends BaseActivity implements View.OnClickListener{

    private static final String TAG = "ACReleaseRecruit";

    public static void start(Context context) {
        Intent starter = new Intent(context, ACReleaseRecruit.class);
        context.startActivity(starter);
    }


    VMReleaseRecruit vmReleaseRecruit;

    TextView tvFileName,tvReleaseDesc,tvSource;


    EditText etContent;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_release_recruit);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("发布招募");
        simpleHead.setOnItemclicklistener(this::finish);

        ZViewHelp.setOnClickListener(this,R.id.tvUpload,this);
        ZViewHelp.setOnClickListener(this,R.id.tvRelease,this);
        tvFileName =  ZViewHelp.findById(this,R.id.tvFileName);
        etContent =  ZViewHelp.findById(this,R.id.etContent);
        tvReleaseDesc =  ZViewHelp.findById(this,R.id.tvReleaseDesc);
        tvSource =  ZViewHelp.setOnClickListener(this,R.id.tvSource,this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmReleaseRecruit = new ViewModelProvider(this).get(VMReleaseRecruit.class);

        vmReleaseRecruit.getLiveErrorToast().observe(this,this::errorToast);

        vmReleaseRecruit.getLiveSucceedToast().observe(this,this::succeedToast);

        vmReleaseRecruit.getLiveWarningToast().observe(this,this::warningToast);

        vmReleaseRecruit.getLiveSelectPicturePath().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

                String fileName = ZFileUtil.getFileName(s);

                tvFileName.setText("文件名:"+fileName);
            }
        });

        vmReleaseRecruit.liveReleaseDesc.observe(this,tvReleaseDesc::setText);

        vmReleaseRecruit.liveSelectSource.observe(this, entityOption -> tvSource.setText(entityOption.getName()));

        vmReleaseRecruit.getLiveNeedLogin().observe(this, aBoolean -> toLogin());
    }

    @Override
    protected void initData() {
        super.initData();
        vmReleaseRecruit.requestReleaseDesc();
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvUpload){

            showPictures();

        }else if (v.getId()==R.id.tvRelease){

            release();

        }else if(v.getId() ==R.id.tvSource){

            showWindow();

        }

    }

    private void release(){

        vmReleaseRecruit.release(etContent.getText().toString());
    }

    private void showPictures(){

        RxPermissions rxPermissions = new RxPermissions(this);

        rxPermissions
                .request(Constant.Permissions.WRITE)
                .subscribe(aBoolean -> {

                    if (aBoolean){
                        PictureSelector.create(ACReleaseRecruit.this)
                                .openGallery(PictureMimeType.ofImage())
                                .maxSelectNum(1)
                                .isCamera(false)
                                .isAndroidQTransform(true)
                                .imageEngine(GlideEngine.createGlideEngine())
                                .forResult(new OnResultCallbackListener<LocalMedia>() {
                                    @Override
                                    public void onResult(List<LocalMedia> result) {
                                        // 结果回调
                                        String path = AndroidVersionUtil.isAndroidQ()? result.get(0).getAndroidQToPath():result.get(0)
                                                .getRealPath();

                                        Log.e(TAG,"path"+path);

                                        vmReleaseRecruit.getLiveSelectPicturePath().setValue(path);

                                    }

                                    @Override
                                    public void onCancel() {
                                        // 取消
                                    }
                                });
                    }else {

                        warningToast(Constant.Tip.NONE_PERMISSION);
                    }
                });




    }

    private void showWindow(){

        WindowWheelPick.getInstance()
                .setOptions(vmReleaseRecruit.source)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {

                        vmReleaseRecruit.liveSelectSource.setValue(entityOption);
                    }
                }).show(getSupportFragmentManager(),"");



    }

}
